#include "service/tracker/StorageChargePriceTracker.h"
#include "core/Logger.h"

#include <iostream>
#include <ctime>
#include <thread>
#include <chrono>

using namespace ems::service::tracker;

void printStats(const StorageChargePriceTracker& tracker) {
    auto stats = tracker.getStats();
    std::cout << "\n=== 充电统计 ===" << std::endl;
    std::cout << "日间充电量: " << stats.dayChargeEnergy << " kWh" << std::endl;
    std::cout << "日间充电成本: " << stats.dayChargeCost << " 元" << std::endl;
    std::cout << "日间平均价: " << stats.getDayAveragePrice() << " 元/kWh" << std::endl;
    std::cout << "\n夜间充电量: " << stats.nightChargeEnergy << " kWh" << std::endl;
    std::cout << "夜间充电成本: " << stats.nightChargeCost << " 元" << std::endl;
    std::cout << "夜间平均价: " << stats.getNightAveragePrice() << " 元/kWh" << std::endl;
    std::cout << "\n总充电量: " << (stats.dayChargeEnergy + stats.nightChargeEnergy) << " kWh" << std::endl;
    std::cout << "总充电成本: " << (stats.dayChargeCost + stats.nightChargeCost) << " 元" << std::endl;
    std::cout << "总平均价: " << stats.getTotalAveragePrice() << " 元/kWh" << std::endl;
    std::cout << "================\n" << std::endl;
}

int main() {
    std::cout << "=== StorageChargePriceTracker 测试程序 ===" << std::endl;
    
    // 创建跟踪器
    StorageChargePriceTracker tracker;
    
    // 配置参数
    tracker.setBatteryCapacity(100.0);  // 100 kWh 电池
    tracker.setDayNightPeriod(6, 18);   // 6:00-18:00 为日间
    tracker.setMaxSocDelta(5.0);        // 最大 5% 增量
    tracker.setStorageCostRate(0.05);   // 储能损耗成本 0.05 元/kWh
    
    std::cout << "\n配置完成：" << std::endl;
    std::cout << "- 电池容量: 100 kWh" << std::endl;
    std::cout << "- 日间时段: 06:00-18:00" << std::endl;
    std::cout << "- 最大 SOC 增量: 5.0%" << std::endl;
    std::cout << "- 储能损耗成本: 0.05 元/kWh（仅用于日间光伏充电）" << std::endl;
    
    // 测试场景 1: 日间光伏充电（SOC 从 30% 到 35%）
    std::cout << "\n--- 场景 1: 日间光伏充电（光伏余电，仅计储能损耗）---" << std::endl;
    std::time_t baseTime = 1697076000;  // 2023-10-12 10:00:00 (日间)
    
    std::cout << "采样 1: SOC=30%, 时间=10:00" << std::endl;
    tracker.onSample(30.0, baseTime, 0.453, 0.678);
    
    std::cout << "采样 2: SOC=35%, 时间=11:00 (+1小时，充电 5%=5kWh)" << std::endl;
    std::cout << "  预期成本: 5kWh × 0.05元/kWh = 0.25元（仅储能损耗）" << std::endl;
    tracker.onSample(35.0, baseTime + 3600, 0.453, 0.678);
    
    printStats(tracker);
    
    // 测试场景 2: 夜间市电充电（分两次，每次 3%）
    std::cout << "\n--- 场景 2: 夜间市电充电（按市电价计费）---" << std::endl;
    std::time_t nightTime = 1697119200;  // 2023-10-12 22:00:00 (夜间)
    
    std::cout << "采样 3: SOC=35%, 时间=22:00" << std::endl;
    tracker.onSample(35.0, nightTime, 0.453, 0.678);
    
    std::cout << "采样 4: SOC=38%, 时间=23:00 (+1小时，充电 3%=3kWh)" << std::endl;
    std::cout << "  预期成本: 3kWh × 0.678元/kWh = 2.034元" << std::endl;
    tracker.onSample(38.0, nightTime + 3600, 0.453, 0.678);
    
    std::cout << "采样 5: SOC=41%, 时间=00:00 (+1小时，充电 3%=3kWh)" << std::endl;
    std::cout << "  预期成本: 3kWh × 0.678元/kWh = 2.034元" << std::endl;
    tracker.onSample(41.0, nightTime + 7200, 0.453, 0.678);
    
    printStats(tracker);
    
    // 测试场景 3: 放电（应该被忽略）
    std::cout << "\n--- 场景 3: 放电测试（应被忽略）---" << std::endl;
    std::cout << "采样 6: SOC=39%, 时间=01:00 (SOC 下降)" << std::endl;
    tracker.onSample(39.0, nightTime + 10800, 0.453, 0.678);
    
    printStats(tracker);  // 统计应该不变
    
    // 测试场景 4: SOC 跳变（应该被过滤）
    std::cout << "\n--- 场景 4: SOC 跳变测试（应被过滤）---" << std::endl;
    std::cout << "采样 7: SOC=48%, 时间=02:00 (跳变 9%，超过阈值 5%)" << std::endl;
    tracker.onSample(48.0, nightTime + 14400, 0.453, 0.678);
    
    printStats(tracker);  // 统计应该不变
    
    // 测试场景 5: 正常小增量充电（接续场景4的SOC 48%）
    std::cout << "\n--- 场景 5: 小增量充电 ---" << std::endl;
    std::cout << "采样 8: SOC=50%, 时间=03:00 (增量 2%，正常)" << std::endl;
    tracker.onSample(50.0, nightTime + 18000, 0.453, 0.678);
    
    printStats(tracker);
    
    // 测试场景 6: 重置统计
    std::cout << "\n--- 场景 6: 重置统计 ---" << std::endl;
    tracker.reset();
    std::cout << "统计已重置" << std::endl;
    
    printStats(tracker);
    
    std::cout << "\n=== 测试完成 ===" << std::endl;
    
    return 0;
}
